package com.infore.dataStatistics.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.StatTask;
import com.infore.common.model.entity.vo.StatTaskVo;
import com.infore.dataStatistics.main.service.StatTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 统计任务 前端控制器
 * </p>
 *
 * @author shaolay
 * @since 2022-12-12
 */
@RestController
@Api(value = "统计任务前端控制器", tags = "统计任务前端控制器")
@RequestMapping("/statTask")
public class StatTaskController extends BaseController {
    private String prefix = "/statTask";

    @Autowired
    private StatTaskService StatTaskService;

    @GetMapping()
    public String StatTaskController() {
        return prefix;
    }

    /**
     * 查询统计任务列表
     *
     * @param stattaskVo 统计任务的vo类
     * @return Msg<Object> 结果集
     * @author shaolay
     * @since 2022-12-12
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "查询统计任务列表", notes = "统计任务列表查询 ")
    public Msg<Object> list(StatTaskVo stattaskVo) {
        //sql查询条件
        Wrapper<StatTask> wrapper = null;
        try {
            wrapper = (Wrapper<StatTask>) WrapperHelp.addCondition(new EntityWrapper<StatTask>(), stattaskVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage());
        }
        //查询方法
        Page<StatTask> eppPage = new EppPage<StatTask>(StatTaskService, stattaskVo, wrapper);
        return ResultUtil.successPage(StatTaskService.selectPage(eppPage, wrapper));
    }

    /**
     * 查询统计任务列表
     *
     * @param stattask 统计任务
     * @return Msg<Object> 结果集
     * @author shaolay
     * @since 2022-12-12
     */
    @ResponseBody
    @ApiOperation(value = "添加统计任务", notes = "统计任务新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Msg<Object> add(@RequestBody StatTask stattask) {
        Wrapper<StatTask> wrapper = new EntityWrapper<StatTask>();
        wrapper.eq("id", stattask.getId());

        wrapper.eq("1", "1");
        List<StatTask> entityList = StatTaskService.selectList(wrapper);
        if (entityList.size() > 0) {
            return ResultUtil.error("重复添加");
        }
        //插入数据
        boolean flag = StatTaskService.insert(stattask);
        return ResultUtil.setResult(flag, stattask.getId().toString());
    }


    /**
     * 更新统计任务方法
     *
     * @param stattask 统计任务
     * @return Msg<Object>
     * @author shaolay
     * @since 2022-12-12
     */
    @ResponseBody
    @ApiOperation(value = "更新统计任务", notes = "统计任务更新")
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Msg<Object> edit(@RequestBody StatTask stattask) {
        //插入数据
        boolean flag = StatTaskService.updateById(stattask);
        return ResultUtil.setResult(flag, stattask.getId().toString());
    }

    /**
     * 删除统计任务
     *
     * @param id
     * @return Msg<Object>
     * @author shaolay
     * @since 2022-12-12
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除统计任务", notes = "删除统计任务")
    public Msg<Object> del(String id) {

        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("id", id);
        boolean flag = StatTaskService.deleteByMap(columnMap);
        return ResultUtil.setResult(flag);
    }
}
